package com.own.component.util.excel.util.read;

import com.own.component.util.excel.util.read.function.resolve.MultipleResolveFunction;
import com.own.component.util.excel.util.read.model.ResolveFileModel;
import com.own.component.util.excel.util.read.util.ResolveFileUtil;
import com.own.component.util.excel.util.read.util.ResolveZipUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Collection;
import java.util.List;

/**
 * ExcelReadIfZipMultiple
 *
 * @author chenxueli
 * @date 2022-10-09 14:16:00
 */
@Slf4j
public class ExcelReadIfZipMultiple<T> extends ExcelReadMultiple<T> {

    /**
     * 读取excel内容
     *
     * @param fileModel       文件信息
     * @param resolveFunction 解析数据的方法
     * @return 解析后的内容信息
     */
    @Override
    public List<T> read(ResolveFileModel fileModel, MultipleResolveFunction resolveFunction) {
        // 设置允许zip解析
        super.allowZip();
        var fileList = ResolveZipUtil.unzip(fileModel);
        var list = fileList
                .stream()
                .map(value -> super.read(value, resolveFunction))
                .flatMap(Collection::stream)
                .toList();
        // 附件信息解析
        ResolveFileUtil.resolve(list, getCurrentConfig(), fileList);
        // 数据返回
        return list;
    }

}
